﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;

namespace SLAssetSystem
{
    public class CsvData : UnityEngine.Object
    {
        private Dictionary<int, List<string>> contents = new Dictionary<int, List<string>>();
        private Dictionary<string, int> keys = new Dictionary<string, int>();
        private Dictionary<int, string> types = new Dictionary<int, string>();

        public void Init(string content)
        {
            string[] Lines = Regex.Split(content, "\r\n", RegexOptions.IgnoreCase);
            string[] header = Lines[0].Split(',');
            for (int i = 0; i < header.Length; i++)
            {
                keys.Add(header[i], i);
            }
            string[] type = Lines[1].Split(',');
            for (int i = 0; i < type.Length; i++)
            {
                types.Add(i, type[i]);
            }
            //末行为空
            for (int i = 2; i < Lines.Length - 1; i++)
            {
                List<string> line = Lines[i].Split(',').ToList();
                contents.Add((i - 2), line.ToList());
            }
        }

        public int GetDataCount()
        {
            return contents.Count;
        }

        public string GetTypeByKey(string key)
        {
            return types[keys[key]];
        }

        public Dictionary<string, int> GetKeys()
        {
            return keys;
        }



        private string GetField(int line, string key)
        {
            if (!contents.ContainsKey(line))
            {
                return "";
            }

            if (!keys.ContainsKey(key))
            {
                return "";
            }

            int index = keys[key];
            if (!string.IsNullOrEmpty(contents[line][index]))
            {
                return contents[line][index];
            }
            return "0";
        }

        private string[] GetArray(int line, string key)
        {
            if (!contents.ContainsKey(line))
            {
                return new string[] { };
            }

            if (!keys.ContainsKey(key))
            {
                return new string[] { };
            }

            int index = keys[key];
            if (string.IsNullOrEmpty(contents[line][index]))
            {
                return new string[] { };
            }
            string[] strs = contents[line][index].Split(';');
            return strs;
        }

        public string GetString(int line, string key)
        {
            return GetField(line, key);
        }

        public int GetInt(int line, string key)
        {
            string content = GetField(line, key);
            return int.Parse(content);
        }

        public bool GetBool(int line, string key)
        {
            string content = GetField(line, key);
            return int.Parse(content) != 0;
        }

        public int[] GetIntArray(int line, string key)
        {
            string[] contents = GetArray(line, key);
            int[] ints = new int[contents.Length];
            for (int i = 0; i < contents.Length; i++)
            {
                ints[i] = int.Parse(contents[i]);
            }
            return ints;
        }

        public float GetFloat(int line, string key)
        {
            string content = GetField(line, key);
            return float.Parse(content);
        }

        public float[] GetFloatArray(int line, string key)
        {
            string[] contents = GetArray(line, key);
            float[] floats = new float[contents.Length];
            for (int i = 0; i < contents.Length; i++)
            {
                floats[i] = float.Parse(contents[i]);
            }
            return floats;
        }


    }
}
